package net.a6polo.service;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import net.a6polo.service.domain.Account;

public class Service extends SQLiteOpenHelper {

	private static final int VERSION = 1;
	
	public Service(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	
	public Service(Context context, String name, CursorFactory factory) {
		super(context, name, factory, VERSION);
	}
	
	public Service(Context context) {
		super(context, "a6polo_db", null, VERSION);
	}


	public long addAccount(Account account) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		//values.put("id", account.getId());
		values.put("uid", account.getUid());
		values.put("user_name", account.getUserName());
		values.put("access_token", account.getAccessToken());
		values.put("refresh_token", account.getRefreshToken());
		values.put("expires_in", account.getType());
		values.put("type", account.getType());
		return db.insert("account", null, values);
		
	}


	public List<Account> getAccounts() {
		// select id, uid, user_name, access_token, refresh_token, expires_in from account;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query("account",  new String[] { "id", "uid", "user_name", "access_token", "refresh_token", "expires_in", "type" }, null, null, null, null, "id asc");
		
		List<Account> accountList = new ArrayList<Account>();
		while (cursor.moveToNext()) {  
            long id = cursor.getLong(cursor.getColumnIndex("id"));  
            long uid = cursor.getLong(cursor.getColumnIndex("uid"));
            String userName = cursor.getString(cursor.getColumnIndex("user_name"));
            String accessToken = cursor.getString(cursor.getColumnIndex("access_token"));
            String refreshToken = cursor.getString(cursor.getColumnIndex("refresh_token"));
            String expiresIn = cursor.getString(cursor.getColumnIndex("expires_in"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            
            Account account = new Account(id, uid, userName, accessToken, refreshToken, expiresIn, type);
            accountList.add(account);
         }
		
		return accountList;
	}


	public Account getAccount(long id) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query("account",  new String[] { "id", "uid", "user_name", "access_token", "refresh_token", "expires_in", "type" }, "id=?", new String[] {id+""}, null, null, "id asc");
		
		Account account = null;
		while (cursor.moveToNext()) {  
            //long id = cursor.getLong(cursor.getColumnIndex("id"));  
            long uid = cursor.getLong(cursor.getColumnIndex("uid"));
            String userName = cursor.getString(cursor.getColumnIndex("user_name"));
            String accessToken = cursor.getString(cursor.getColumnIndex("access_token"));
            String refreshToken = cursor.getString(cursor.getColumnIndex("refresh_token"));
            String expiresIn = cursor.getString(cursor.getColumnIndex("expires_in"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            
            account = new Account(id, uid, userName, accessToken, refreshToken, expiresIn, type);
            break;
         }
		
		return account;
	}


	public List<Account> getAccountByType(int type) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query("account",  new String[] { "id", "uid", "user_name", "access_token", "refresh_token", "expires_in", "type" }, "type=?",  new String[] {type+""}, null, null, "id desc");
		
		List<Account> accountList = new ArrayList<Account>();
		while (cursor.moveToNext()) {  
            long id = cursor.getLong(cursor.getColumnIndex("id"));  
            long uid = cursor.getLong(cursor.getColumnIndex("uid"));
            String userName = cursor.getString(cursor.getColumnIndex("user_name"));
            String accessToken = cursor.getString(cursor.getColumnIndex("access_token"));
            String refreshToken = cursor.getString(cursor.getColumnIndex("refresh_token"));
            String expiresIn = cursor.getString(cursor.getColumnIndex("expires_in"));
            //int type = cursor.getInt(cursor.getColumnIndex("type"));
            
            Account account = new Account(id, uid, userName, accessToken, refreshToken, expiresIn, type);
            accountList.add(account);
         }
		
		return accountList;
	}


	public Account getLastAccountByType(int type) {
		List<Account> list = getAccountByType(type);
		if(list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	public boolean update(Account account) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("uid", account.getUid());
		values.put("user_name", account.getUserName());
		values.put("access_token", account.getAccessToken());
		values.put("refresh_token", account.getRefreshToken());
		values.put("expires_in", account.getType());
		values.put("type", account.getType());
		int result = db.update("account", values, "id=?", new String[] {account.getId()+""});
		return (result > 0)?true:false;
	}

	
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table account (id INTEGER primary key autoincrement, uid long , user_name varchar(20), access_token varchar(30), refresh_token varchar(20), expires_in varchar(20), type int(3))";
        db.execSQL(sql);
	}

	
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		  String sql = "drop table if exists account";
          db.execSQL(sql);
          onCreate(db);
	}

}
